package com.xukai.springboot.datalayer.statis;

import com.xukai.springboot.entity.statistics.t_statis_play_item;
import com.xukai.springboot.entity.statistics.t_statis_play_main;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StatisPlayMapper {

    /**
     * 获取播放列表
     */
    @SelectProvider(type = StatisPlayMapperProvider.class, method = "GetPlayList")
    public List<t_statis_play_main> GetPlayList(@Param("name") String name, @Param("state") int state, @Param("start") int start, @Param("pageSize") int pageSize);


    /**
     * 获取播放列表基本信息
     */
    @Select(" select id, `name`,state,displayTime,animation  from t_statis_play_main  where id=#{id}")
    public t_statis_play_main GetPlay(@Param("id") int id);


    /**
     * 新建播放列表
     */
    @Insert(" INSERT INTO t_statis_play_main(  `name`,state,displayTime,animation,createTime )\n" +
            "values (#{name},#{state},#{displayTime},#{animation},#{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void AddPlay(t_statis_play_main data);

    /**
     * 修改播放列表基本信息
     */
    @Update(" UPDATE t_statis_play_main set   `name`=#{name},state=#{state},displayTime=#{displayTime},animation=#{animation} where id=#{id}")
    public void UpdatePlay(t_statis_play_main data);

    /**
     * 删除播放列表基本信息
     */
    @Delete(" delete from t_statis_play_main where id=#{id}")
    public void DelPlay(@Param("id") int id);

    /**
     * 获取播放列表数据总数
     */
    @SelectProvider(type = StatisPlayMapperProvider.class, method = "GetPlayCount")
    @ResultType(Integer.class)
    public int GetPlayCount(@Param("name") String name, @Param("state") int state);

    /**
     * 获取场景列表
     */
    @SelectProvider(type = StatisPlayMapperProvider.class, method = "GetPlayItemList")
    public List<t_statis_play_item> GetPlayItemList(@Param("mainID") int mainID, @Param("start") int start, @Param("pageSize") int pageSize);


    /**
     * 获取场景总数
     */
    @Select("select count(id) from t_statis_play_item  where mainID=#{mainID}")
     @ResultType(Integer.class)
    public int GetPlayItemCount(@Param("mainID") int mainID);

    /**
     * 添加场景
     */
    @Insert(" INSERT INTO t_statis_play_item ( sceneID,sceneName ,sort,mainID ) " +
            "values ( #{sceneID},#{sceneName} ,#{sort},#{mainID})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    public void AddPlayItem(t_statis_play_item data);

    /**
     * 删除场景
     */
    @Delete(" Delete from t_statis_play_item where id=#{id}")
    public void DelPlayItem(@Param("id") int id);


    /**
     * 判断场景是否存在
     *
     * @param id
     * @return
     */
    @Select(" select count(id) from t_statis_play_item where sceneID=#{sceneID} and mainID=#{mainID}")
    @ResultType(Integer.class)
    public int sceneIsExist(@Param("sceneID") int id,@Param("mainID") int mainID);

    /**
     * 验证是否存在
     *
     * @param id
     * @param name
     * @return
     */
    @SelectProvider(type = StatisPlayMapperProvider.class, method = "IsExist")
    @ResultType(Integer.class)
    public int IsExist(@Param("id") int id, @Param("name") String name);

    /**
     * 修改场景排序
     */
    @Update(" update t_statis_play_item set sort=#{sort} where id=#{id}")
    public void UpdateItemSort(@Param("id") int id, @Param("sort") int sort);

    /**
     * 获取最高排序
     *
     * @param id
     * @return
     */
    @Select("select IFNULL(max(sort),0)  from t_statis_play_item where mainID=#{mainID}")
    @ResultType(Integer.class)
    public int GetItemMaxSort(@Param("mainID") int mainID);


    class StatisPlayMapperProvider {

        /**
         * 验证是否存在
         *
         * @param id
         * @param name
         * @return
         */
        public String IsExist(int id, String name) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select Count(id) from t_statis_play_main");
            sb.append(" where  `name`=#{name}");
            if (id > 0) {
                sb.append(" and id <> #{id}");
            }
            return sb.toString();
        }

        /**
         * 获取播放列表
         */
        public String GetPlayList(String name, int state, int start, int pageSize) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select id, `name`,state from t_statis_play_main ");
            sb.append(" where 1=1");
            if (!name.isEmpty()) {
                //sb.append(" and  `name` like '%#{name}%'");
                sb.append(" and dataSourceName like concat(concat('%',#{name}),'%') ");
            }
            if (state > -1) {
                sb.append(" and state=#{state}");
            }
            sb.append(" order by createTime desc ");
            if (pageSize > -1) {
                sb.append(" limit #{start},#{pageSize}");
            }
            return sb.toString();
        }

        /**
         * 获取播放数据总数
         */
        public String GetPlayCount(String name, int state) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select count(id) total from t_statis_play_main ");
            sb.append(" where 1=1");
            if (!name.isEmpty()) {
                //sb.append(" and  `name` like '%#{name}%'");
                sb.append(" and dataSourceName like concat(concat('%',#{name}),'%') ");
            }
            if (state > -1) {
                sb.append(" and state=#{state}");
            }

            return sb.toString();
        }

        /**
         * 获取播放列表item
         *
         * @param id
         * @param start
         * @param pageSize
         * @return
         */
        public String GetPlayItemList(int id, int start, int pageSize) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select id, sceneID,sceneName,sort from t_statis_play_item  where mainID=#{mainID}");
            sb.append(" order by sort");
            if (pageSize > -1) {
                sb.append(" limit #{start}, #{pageSize}");
            }

            return sb.toString();
        }



    }

}
